/*
 * UpdatePassFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import com.insigma.yanwenlong.bookkeeping.pojo.Login;
import com.insigma.yanwenlong.bookkeeping.service.LoginService;
import com.insigma.yanwenlong.bookkeeping.service.LoginServiceImpl;
import com.insigma.yanwenlong.bookkeeping.util.EncrptUtils;

/**
 *
 * @author  __USER__
 */
public class UpdatePassFrame extends javax.swing.JFrame {
	private LoginService loginService = new LoginServiceImpl();

	/** Creates new form UpdatePassFrame */
	public UpdatePassFrame() {
		this.setTitle("密码修改");
		initComponents();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//初始化登录人员
		initPeople();
	}

	private void initPeople() {
		try {
			List<Login> logins = loginService.findLoginAccounts();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobUserName
					.getModel();
			for (Login login : logins) {
				dcbm.addElement(login);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		cobUserName = new javax.swing.JComboBox();
		jLabel2 = new javax.swing.JLabel();
		jpfPass = new javax.swing.JPasswordField();
		jpfRepeatPass = new javax.swing.JPasswordField();
		jLabel3 = new javax.swing.JLabel();
		btnOK = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u7528\u6237\u540d\uff1a");

		jLabel2.setText("\u5bc6\u7801\uff1a");

		jLabel3.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");

		btnOK.setText("\u786e\u5b9a");
		btnOK.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnOKActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap(25, Short.MAX_VALUE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel3)
														.addComponent(jLabel1)
														.addComponent(jLabel2))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																false)
														.addComponent(
																jpfRepeatPass,
																javax.swing.GroupLayout.Alignment.LEADING,
																0, 0,
																Short.MAX_VALUE)
														.addComponent(
																jpfPass,
																javax.swing.GroupLayout.Alignment.LEADING,
																0, 0,
																Short.MAX_VALUE)
														.addComponent(
																cobUserName,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																98,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()).addGroup(
								layout.createSequentialGroup().addGap(60, 60,
										60).addComponent(btnOK)
										.addContainerGap(80, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(28, 28, 28)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																cobUserName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(30, 30, 30)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jpfPass,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jLabel2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																15,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(29, 29, 29)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jpfRepeatPass,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel3))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												35, Short.MAX_VALUE)
										.addComponent(btnOK).addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击确定，进行修改密码
	private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
		Login login = (Login)cobUserName.getSelectedItem();
		String psd = new String(jpfPass.getPassword());
		String RepeatPsd = new String(jpfRepeatPass.getPassword());
		
		if("".equals(psd) || "".equals(RepeatPsd)) {
			JOptionPane.showMessageDialog(this, "修改密码不能为空");
			return;
		}
		
		Login login2 = new Login();
		login2.setName(login.getName());
		login2.setId(login.getId());
		login2.setPassword(EncrptUtils.encryptToMD5(psd));
		
		if(psd.equals(RepeatPsd)) {
			try {
				int rowAffected = loginService.UpdatePsd(login2);
				if(rowAffected > 0) {
					JOptionPane.showMessageDialog(this, "密码修改成功");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			JOptionPane.showMessageDialog(this, "两次密码不相同");
			return;
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new UpdatePassFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnOK;
	private javax.swing.JComboBox cobUserName;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPasswordField jpfPass;
	private javax.swing.JPasswordField jpfRepeatPass;
	// End of variables declaration//GEN-END:variables

}